package cn.outsourcing.supermarket.converter.transform;

import cn.outsourcing.supermarket.common.constants.CommonConstants;

import java.math.BigDecimal;

/**
 * 用户名转化器
 *
 * @author gzkemays
 * @since 2021/11/10 13:54
 */
public class BalanceTransform {
  public static String transformBalance(BigDecimal balance) {
    String balanceStr = balance.toString().trim();
    int fullStop = balanceStr.indexOf(".");
    String end = balanceStr.substring(fullStop);
    System.out.println(end);
    int length = balanceStr.length();
    if (length > 4) {
      for (int i = 1; i < length; i = i + 4) {
        String nextStr = balanceStr.substring(i);
        boolean isSplit = nextStr.equals(CommonConstants.COMMA_SEPARATOR);
        if (!isSplit) {
          balanceStr = balanceStr.substring(0, i) + "," + balanceStr.substring(i);
        }
        if (end.equals(".00")) {
          balanceStr = balanceStr.replace(end, "");
        }
      }
      return balanceStr;
    }
    return balanceStr;
  }

  public static void main(String[] args) {
    System.out.println(transformBalance(new BigDecimal("300052.00")));
  }
}
